package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel_封装参数为对象传给前端.SubjectData;
import com.atguigu.eduservice.entity.subject_封装参数为对象传给前端.OneSubject;
import com.atguigu.eduservice.entity.subject_封装参数为对象传给前端.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            System.out.println("================================");
            e.printStackTrace();
            System.out.println("================================");

        }
    }

    //查询第一二级的课程，返回一级课程
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询条件
        QueryWrapper<EduSubject> EduSubjectqueryWrapperOne = new QueryWrapper<>();
        EduSubjectqueryWrapperOne.eq("parent_id", "0");
        //mybatis-baseMapper搜索，返回OneSubject
        List<EduSubject> OneSubjectList = baseMapper.selectList(EduSubjectqueryWrapperOne);


        //查询所有二级分类，条件："parent_id"不为0即是二级分类，"parent_id"是0说明是一级分类
        QueryWrapper<EduSubject> EduSubjectqueryWrapperTwo = new QueryWrapper<>();
        EduSubjectqueryWrapperTwo.ne("parent_id", "0");
        List<EduSubject> TwoSubjectlist = baseMapper.selectList(EduSubjectqueryWrapperTwo);


        List<OneSubject> finalSubjectList = new ArrayList<>();


        //将OneSubjectList拆开，然后存到单独的onesubject中，实体与实体时间互存，可以用BeanUtils.copyProperties(source,target)
        for (int i = 0; i < OneSubjectList.size(); i++) {
            EduSubject eduSubject = OneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);
            finalSubjectList.add(oneSubject);

            //在一级分类的循环中查找所有的二级分类
            //创建List集合封装每一个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < TwoSubjectlist.size(); j++) {
                EduSubject twoSubject = TwoSubjectlist.get(j);
                //判断二级分类parentid和一级分类id是否一样
                if (twoSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject everytwoSubject = new TwoSubject();
                    //修补bug，在if种，判断二级分类的parentid和一级分类id是否一样，一样就把之前twoFinalSubjectList获取到的每一个
                    //twoSubject存放到everytwoSubject中，everytwoSubject是TwoSubject类的对象，才能 twoFinalSubjectList.add
                    BeanUtils.copyProperties(twoSubject, everytwoSubject);
                    twoFinalSubjectList.add(everytwoSubject);
                }

            }
            //把一级下面所有二级分类放到一级分类里面

            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;

    }

}